package com.xukai.springboot.datalayer.patient;

import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
import com.xukai.springboot.entity.patient.t_patient;
import com.xukai.springboot.entity.patient.t_patient_wechat;
import com.xukai.springboot.entity.response.account.WXAccountResponse;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface PatientMapper {

    /**
     * 根据微信OpenID获取患者信息
     */

    @Select("select p.id,p.contact,p.createTime,p.destribe,p.email,p.`name`,p.state,pw.sex,pw.city,pw.country,pw.headimgurl,pw.nickName,pw.headimgurl " +
            "from t_patient_wechat pw join t_patient as p on pw.openID=p.openID where pw.openID=#{openID}")
    public WXAccountResponse WXAccount(@Param("openID") String openID);


    /**
     * 新建微信信息
     */
    @Insert("INSERT into t_patient_wechat(openID,nickName,state,sex,province,city,country,headimgurl,unionid) \n" +
            "VALUES (#{openID},#{nickName},#{state},#{sex},#{province},#{city},#{country},#{headimgurl},#{unionid})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void CreatePatientWechat(t_patient_wechat param);


    /**
     * 修改微信信息
     */
    @Update("UPDATE t_patient_wechat set nickName=#{nickName},sex=#{sex},province=#{province}" +
            ",city=#{city},country=#{country},headimgurl=#{headimgurl}" +
            " where openID==#{openID}")
    public void UpdatePatientWeChat(@Param("openID") String openID, @Param("nickName") String nickName, @Param("sex") int sex, @Param("province") String province, @Param("city") String city,
                                    @Param("country") String country, @Param("headimgurl") String headimgurl);


    /**
     * 修改微信数据状态
     */
    @Update("UPDATE t_patient_wechat set state=#{state} where id=#{id}")
    public void UpdatePatientWeChatState(@Param("id") int id, @Param("stata") int state);


    /**
     * 删除微信数据
     */
    @Delete("Delete t_patient_wechat where id=#{id}")
    public void DelPatinetWerChat(@Param("id") int id);


    /**
     * 新建患者信息
     */
    @Insert("INSERT INTO t_patient(`name`,destribe,createTime,contact,email,state) " +
            "VALUES(#{name},#{destribe},#{createTime},#{contact},#{email},#{state})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void CreatePatient(t_patient Param);


    /**
     * 修改患者信息
     */
    @Update("UPDATE t_patient set `name`=#{name},destribe=#{destribe},contact=#{contact},email=#{email} where id=#{id} ")
    public void UpdatePatient(@Param("name") String name, @Param("destribe") String destribe, @Param("contact") String contact, @Param("email") String email,
                              @Param("id") int id);

    /**
     * 获取患者信息
     */
    @Select("Select * from t_patient where id=#{id}")
    public t_patient GetPatient(@Param("id") int id);


    /**
     * 删除患者信息
     */
    @Delete("Delete table t_patient where id=#{id}")
    public void DelPatient(@Param("id") int id);


    /**
     * 修改患者状态
     */
    @Update("UPDATE t_patient set state=#{state} where id=#{id} ")
    public void UpdatePatientState(@Param("state") int state, @Param("id") int id);

    /**
     * 获取患者课程信息
     */

    /**
     * 获取患者订单信息
     */

    /**
     * 获取患者列表
     */
    @SelectProvider(type = PatientMapperProvider.class, method = "GetPatientList")
    public List<t_patient> GetPatientList(@Param("PageSize") int PageSize, @Param("StartIndex") int StartIndex, @Param("PatientName") String PatientName);

    /**
     * 获取患者训练计划列表
     */

    /**
     *获取患者历史训练信息
     */

    /**
     * PatinetMapper处理类
     */
    class PatientMapperProvider {

        /**
         * 获取内容列表
         *
         * @return
         */
        public String GetPatientList(int StartIndex, int PageSize, String PatientName) {

            StringBuilder sb = new StringBuilder();
            sb.append(" select id,`name`,destribe,createTime,createTime,contact,email,state,openID");
            sb.append(" from t_patient");
            if (!PatientName.isEmpty()) {
                //sb.append(" where  `name` like '%#{PatientName}%'");
                sb.append(" and dataSourceName like concat(concat('%',#{PatientName}),'%') ");
            }
            sb.append(" order by `name` desc");
            sb.append(" limit #StartIndex ,#PageSize");

            return sb.toString();
        }
    }
}


